<?PHP
defined('_XR_EXEC') or die();
/**
 * 控制器处理类 
 */
class account_buy_Controller extends controller_user1{
	function __construct(){
		parent::__construct();
	 	$this->assign('nav','选菜');
		$this->assign('account_menu1','1');
	}
	
	##################################
	/* 购菜 */
	################################## 
	function index__(){ 
	  
		$m_member = factory::module('member');
		$m_trans = factory::module('trans');
		
		$this->assign('rs',$m_member->getuserinfo()); 
		$this->assign('balance',$m_trans->getbalance( $m_member->getuserid() ));
	}
	function index_list__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			#得到传递过来的参数
			$_POST=format::addslash_deep($_POST);
			if(!isset($_POST['start']))			$_POST['start']			= 0;
			if(!isset($_POST['limit']))			$_POST['limit']			= 1000;
			if(!isset($_POST['sort']))				$_POST['sort']			= 'a.id';
			if(!isset($_POST['dir']))				$_POST['dir']				= 'DESC';
			if(!isset($_POST['sortval']))     $_POST['sortval']  = '';
		//	if(!isset($_POST['combotype']))   $_POST['combotype']  = '0';
		//	if($_POST['combotype'] != '0'){
		//		$_POST['combotype'] = '1';
		//	}else{
		//		$_POST['combotype'] = '0';
		//	}
			$nowdate = date('Y-m-d H:i:s');
  	  #查询条件
			$sql_where = ' where a.deleted=0 and a.publishs=1 and a.bdate<="'.$nowdate.'" and a.edate>="'.$nowdate.'" ';
			if($_POST['sortval'] != ''){
				switch($_POST['sortval']){
					case "1":
						$_POST['sort'] = 'price';
						$_POST['dir'] = 'DESC';
						break;
					case "2":
						$_POST['sort'] = 'price';
						$_POST['dir'] = 'ASC';
						break;
					case "3":
						$_POST['sort'] = 'sendedate';
						$_POST['dir'] = 'DESC';
						break;
					case "4":
						$_POST['sort'] = 'sendedate';
						$_POST['dir'] = 'ASC';
						break;
					case "5":
						$_POST['sort'] = 'created';
						$_POST['dir'] = 'DESC';
						break;
					default://6
						$_POST['sort'] = 'created';
						$_POST['dir'] = 'ASC';
						break;	
				}
			}
			#排序信息
			$sql_order = " ORDER BY ".$_POST['sort'].' '.$_POST['dir']." LIMIT ".$_POST['start'].", ".$_POST['limit'];
			#先查总数 
			$sql = "
			select 
			count(a.id) 
			from `t_combo` a 
			".$sql_where ;  
			$db = factory::getDBO();
			$rows = $db->LoadResult($sql); 
			if($rows){
				#检索出具体数据
				$sql = "select
					a.*
				from `t_combo` a  
				".$sql_where.$sql_order;
				$rs = $db->LoadAssocList($sql);
				foreach($rs as &$val){
					$val['combo_item'] = $db->loadassoclist('select a.*,b.name as vege_name,b.price from t_combo_item a left join t_vege b on a.vege_id=b.id where a.combo_id="'.$val['id'].'" ');
				}
				#输出结果
				echo '({"total":"'.$rows.'","results":'.json_encode($rs).'})';
			}else{
				echo '({"total":"0","results":[]})';
			}
			exit();
		}
		exit();
	}
	
	##################################
	/* 固定套餐购买 */
	##################################
	public function fixbuy__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{ 
			$_POST = format::addslash_deep($_POST);
			$id = intval($_POST['id']);
			$db = factory::getDBO();
			$db->begin();
			$m_member = factory::module('member');
			$user_id=$m_member->getuserid();
			//判断是否可以购买
			$nowdate = date('Y-m-d H:i:s');
			$rs_combo = $db->loadassoc('select a.* from `t_combo` a where id="'.$id.'" and  a.deleted=0  and a.publishs=1  and a.type=0 and a.bdate<="'.$nowdate.'" and a.edate>="'.$nowdate.'" ');
			if(!$rs_combo){
				exit('{success:0,msg:"未找到商品信息"}');
			}
			$rs_cart = $db->loadassoc('select * from t_cart where combo_id="'.$id.'" and user_id="'.$user_id.'" and deleted=0 ');
			if(!$rs_cart){
				//新增到购物车
				$db->insert('cart',array(
					'user_id'=>$user_id,
					'combo_id'=>$id,
					'price'=>$rs_combo['price'],
					'nums'=>'1',
					'price_total'=>$rs_combo['price'],
					'updated'=>'now()',
					'created'=>'now()'
				));
			}else{
				//增加一个数量
				$db->update('cart',array(
					'id'=>$rs_cart['id'],
					'combo_id'=>$id,
					'price'=>$rs_combo['price'],
					'nums'=>($rs_cart['nums']+1),
					'price_total'=>(($rs_cart['nums']+1)*$rs_combo['price']),
					'updated'=>'now()'
				));
			}
			$cartinfo = factory::module('cart')->renewGetCart($user_id);
			$r_nums = 0;
			$r_price_total = 0;
			foreach($cartinfo as $v){
				$r_nums += $v['nums'];
				$r_price_total += $v['price_total'];
			}
			$db->commit();
			exit('{success:1,nums:"'.$r_nums.'",price_total:"'.$r_price_total.'"}');
		}
		exit();
	}

	##################################
	/* 组合套餐选菜购买 */
	##################################
	public function sltbuy__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{ 
			$vegeids = format::addslash_json(json_decode( stripslashes($_POST['vegeids']) )); 
			$_POST=format::addslash_deep($_POST);
			$id = $_POST['combo_id']; 
			$db = factory::getDBO();
			$db->begin();
			$m_member = factory::module('member');
			$user_id=$m_member->getuserid();
			//判断是否可以购买
			$nowdate = date('Y-m-d H:i:s');
			$rs_combo = $db->loadassoc('select a.* from `t_combo` a where id="'.$id.'" and  a.deleted=0  and a.publishs=1  and a.type=1 and a.bdate<="'.$nowdate.'" and a.edate>="'.$nowdate.'" ');
			if(!$rs_combo){
				exit('{success:0,msg:"未找到商品信息"}');
			}
			//组合套餐，无论怎样都以新的购物 新增到购物车
			$cart_id = $db->insert('cart',array(
				'user_id'=>$user_id,
				'combo_id'=>$id,
				'price'=>'0',
				'nums'=>'1',
				'price_total'=>'0',
				'updated'=>'now()',
				'created'=>'now()'
			));
			//插入明细
			$nums_total = 0;
			$price_total = 0;
			foreach($vegeids as $v){
				$nums_total += intval($v['vege_num']);
				$price = $db->loadresult('select price from t_vege where id="'.$v['vege_id'].'" ');
				$price_total += $price*intval($v['vege_num']);
				$db->insert('cart_item',array(
					'cart_id'=>$cart_id,
					'vege_id'=>$v['vege_id'],
					'vege_num'=>intval($v['vege_num'])
				));
			}
			
			//判断蔬菜份数是否达到最小选菜份数
			if($nums_total - $rs_combo['min_num']>=0 || $price_total>=$rs_combo['min_price']){
			
			}else{
				exit('{success:0,msg:"最少选菜 '.$rs_combo['min_num'].' 份 或 最少选菜总价 '.$rs_combo['min_price'].' 元"}');
			}
			
		 
			
			//更新金额
			$db->update('cart',array(
				'id'=>$cart_id,
				'price'=>$price_total,
				'price_total'=>$price_total
			));
			$cartinfo = factory::module('cart')->renewGetCart($user_id);
			$r_nums = 0;
			$r_price_total = 0;
			foreach($cartinfo as $v){
				$r_nums += $v['nums'];
				$r_price_total += $v['price_total'];
			}
			$db->commit();
			exit('{success:1,nums:"'.$r_nums.'",price_total:"'.$r_price_total.'"}');
		}
		$_GET=format::addslash_deep($_GET);
		$id = intval($_GET['id']);
		$db = factory::getDBO();
		$m_member = factory::module('member');
		$user_id=$m_member->getuserid();
		//判断是否可以购买
		$nowdate = date('Y-m-d H:i:s');
		$rs_combo = $db->loadassoc('select a.* from `t_combo` a where id="'.$id.'" and   a.deleted=0  and a.publishs=1  and a.type=1 and a.bdate<="'.$nowdate.'" and a.edate>="'.$nowdate.'" ');
		if(!$rs_combo){
			exit('未找到商品信息');
		}
		$rs_combo['combo_item'] = $db->loadassoclist('select a.*,b.name as vege_name,b.price from t_combo_item a left join t_vege b on a.vege_id=b.id where a.combo_id="'.$rs_combo['id'].'" ');
		$this->assign('rs_combo',$rs_combo);
		# 菜品信息
		$this->assign('rs_veges',$db->loadassoclist('select * from `t_vege` where deleted=0 ','id'));
	}
	
	
	
	
	
	
	
	
	 
     
	 
}
?>